package com.itboye.lottery.activity

import android.content.Intent
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.handmark.pulltorefresh.library.ObservableScrollView
import com.handmark.pulltorefresh.library.PullToRefreshBase
import com.itboye.lottery.R
import com.itboye.lottery.activity.follow.HotPeopleInfoActivity
import com.itboye.lottery.adapter.BangDanAdapter
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.bean.PeopleBean
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.ResponseCallback
import kotlinx.android.synthetic.main.activity_chong_zhi_ming_xi_list.*

class BangDanActivity : BaseActivity(), ResponseCallback {


    var type = 0
    var rv_bangdan: RecyclerView? = null
    var ar = ArrayList<PeopleBean>()
    var page = 1
    var pageSize = 10
    var userPresenter: UserPresenter = UserPresenter(this)
    override fun getLayoutId(): Int {
        return R.layout.activity_bang_dan
    }

    private var adapter: BangDanAdapter? = null

    override fun initData() {
        type = intent.getIntExtra("type", 0)
        when (type) {
            1 -> {
                setTitle("红人榜")
            }
            else -> {
            }
        }
        getHotPeopleList()
        adapter = BangDanAdapter(this, ar)
        adapter!!.setOnItemClickListener({ view, position ->
            startActivity(Intent(BangDanActivity@ this, HotPeopleInfoActivity::class.java).putExtra("userId", ar[position].uid))
        })
        rv_bangdan?.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        rv_bangdan?.layoutManager = (LinearLayoutManager(BangDanActivity@ this))
        rv_bangdan?.adapter = adapter

        pull_to_refresh_scrollView.mode = PullToRefreshBase.Mode.BOTH
        pull_to_refresh_scrollView.setOnRefreshListener(object : PullToRefreshBase.OnRefreshListener2<ObservableScrollView> {
            override fun onPullDownToRefresh(refreshView: PullToRefreshBase<ObservableScrollView>) {
                page = 1
                getHotPeopleList()
            }

            override fun onPullUpToRefresh(refreshView: PullToRefreshBase<ObservableScrollView>) {
                page++
                getHotPeopleList()
            }
        })
    }

    private fun getHotPeopleList() {
        userPresenter.getHotPeopleList(page, pageSize)
    }

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.tv_is_follow -> {
                //跟单
            }
            else -> {
            }
        }
    }


    override fun success(o: Any, requestSuccessType: String) {
        pull_to_refresh_scrollView.onRefreshComplete()
        if (requestSuccessType == userPresenter.getHotPeopleList_SUCCESS) {
            var peopleArray = o as ArrayList<PeopleBean>
            if (page==1) {
                ar.clear()
            }
            ar.addAll(peopleArray)
            adapter!!.notifyDataSetChanged()
        }
    }

    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        pull_to_refresh_scrollView.onRefreshComplete()
        if (requestErrorType == userPresenter.getHotPeopleList_FAIL) {
            ByAlert.alert(msg)
        }
    }


}
